import React, { useEffect, useState } from "react";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
NProgress.configure({ speed: 500, trickleSpeed: 100 });


// ...existing imports...
import { getSidebarData } from "@/components/database/index"; // 如果和本文件同级可省略

export function SidebarDataProvider({ children }: { children: React.ReactNode }) {
  const [data, setData] = useState<{ versions: string[]; navMain: any[] }>({ versions: [], navMain: [] });
  const SidebarDataContext = React.createContext<{ versions: string[]; navMain: any[] }>({ versions: [], navMain: [] });
  useEffect(() => {
    if (data.versions.length && data.navMain.length) return;

    NProgress.start();
    getSidebarData()
      .then(res => {
        if (res) {
          setData(res);
          localStorage.setItem("sidebarData", JSON.stringify(res));
        }
      })
      .catch(() => setData({ versions: [], navMain: [] }))
      .finally(() => NProgress.done());
  }, []);

  return (
  <SidebarDataContext.Provider value={data}>
    {children}
  </SidebarDataContext.Provider>
);
}